﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VietThuongLMS.ViewModels;
using System.Web.Mvc;
using System.Data.Entity;
using VietThuongLMS.Utils;

/**
 * <Jason>
 * <Date: 20131114>
 * 
 */
namespace VietThuongLMS.Services
{

    public class GhiChuService
    {
        public List<GhiChu> GetGhiChuList(int maNhanVien)
        {
            try
            {
                using (LMSDataEntities context = new LMSDataEntities())
                {
                    List<GhiChu> list = context.GhiChus.Where(m => m.MaNhanVien == maNhanVien && m.Deleted == Utils.Constant.GENERAL_STATUS_NORMAL).ToList();
                    //List<GhiChu> list = context.GhiChus.Include("LoaiTaiKhoan").Where(m => m.TaiKhoan.MaLoaiTaiKhoan == Utils.Constant.ACCOUNT_ROLE_TEACHER).ToList();
                    return list;
                }
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("GhiChuServices::GetGhiChuList", ex.StackTrace, ex.Message);
                return null;
            }
        }

        public static GhiChuViewModel GetGhiChuList(int pageNum, int pageIndex)
        {
            try
            {
                using (LMSDataEntities context = new LMSDataEntities())
                {
                    //List<GhiChu> list = context.GhiChus.Where(m => m.MaNhanVien == maNhanVien).OrderBy(m => m.NgayLap).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                    List<GhiChu> list = context.GhiChus.Join(context.NhanViens,
                        gc => gc.MaNhanVien,
                        nv => nv.ID,
                        (gc, nv) => gc).Where(gc => gc.Deleted == Utils.Constant.GENERAL_STATUS_NORMAL).OrderBy(gc => gc.NgayLap).ToList();

                    foreach (GhiChu gc in list)
                    {
                        gc.NhanVienReference.Load();
                        gc.NhanVienReference.Value.TaiKhoanReference.Load();
                    }

                    int total = list.Count();

                    return new GhiChuViewModel(list, total);
                }
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("GhiChuServices::GetGhiChuList", ex.StackTrace, ex.Message);
                return new GhiChuViewModel();
            }
        }

        public static bool InsertGhiChu(GhiChu comp)
        {
            try
            {
                using (LMSDataEntities context = new LMSDataEntities())
                {
                    comp.NgayLap = DateTime.Now;
                    context.AddToGhiChus(comp);
                    context.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("GhiChuServices::InsertGhiChu", ex.StackTrace, ex.Message);
                return false;
            }
        }

        public static bool DeleteGhiChu(int id)
        {
            try
            {
                using (LMSDataEntities context = new LMSDataEntities())
                {
                    GhiChu obj = context.GhiChus.Where(c => c.ID == id).FirstOrDefault();
                    if (obj != null)
                    {
                        obj.Deleted = Utils.Constant.GENERAL_STATUS_DELETED;
                        //context.GhiChus.DeleteObject(obj);
                        context.SaveChanges();
                        return true;
                    }
                    return false;
                }
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("GhiChuServices::DeleteGhiChu", ex.StackTrace, ex.Message);
                return false;
            }
        }

        public static GhiChu GetGhiChuById(int id)
        {
            try
            {
                using (LMSDataEntities context = new LMSDataEntities())
                {
                    GhiChu gc = new GhiChu();

                    gc = context.GhiChus.Join(context.NhanViens,
                        g => g.MaNhanVien,
                        nv => nv.ID,
                        (g, nv) => g).Where(g => g.ID == id).FirstOrDefault();

                    gc.NhanVienReference.Load();
                    gc.NhanVienReference.Value.TaiKhoanReference.Load();
                    return gc;
                }
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("GhiChuServices::GetGhiChuById", ex.StackTrace, ex.Message);
                return null;
            }
        }

        public static bool Edit(GhiChu newModel)
        {
            try
            {
                using (LMSDataEntities context = new LMSDataEntities())
                {
                    GhiChu model = context.GhiChus.Where(c => c.ID == newModel.ID).FirstOrDefault();

                    if (model != null)
                    {
                        model.TieuDe = newModel.TieuDe;
                        model.NoiDung = newModel.NoiDung;
                        model.NgayLap = DateTime.Now;

                        context.SaveChanges();
                        return true;
                    }
                    return false;
                }
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("GhiChuServices::Edit", ex.StackTrace, ex.Message);
                return false;
            }
        }

        ////authenticate
        //public static GhiChu GetGhiChu(string tenGhiChu)
        //{
        //    LMSDataEntities context = new LMSDataEntities();
        //    try
        //    {
        //        return context.GhiChus.Single(a => a.TenGhiChu.Equals(tenGhiChu));

        //    }
        //    catch (Exception ex)
        //    {
        //        DCS.ComLib.File.Log.Write("GhiChuServices::GetGhiChu", ex.StackTrace, ex.Message);
        //        return null;
        //    }
        //    finally
        //    {
        //        context.Dispose();
        //    }
        //}        


    }
}
